package com.symaster.common.geom;

import com.symaster.common.geom.entity.Point;
import com.symaster.common.geom.entity.Polygon;
import com.symaster.common.geom.entity.Vector;
import com.symaster.common.geom.util.PointUtil;

import java.util.*;

/**
 * @author yinmiao
 * @date 2022-03-29 09:06
 */
public class PolygonPanelItem extends Polygon {

    private String srcImagePath;
    private Polygon srcPolygon;
    private Double angle;
    private Double scale;
    private Map<String, PolygonPanelItem> polygonPanelItemMap;
    private Map<String, Vector> vectorMap;

    public PolygonPanelItem(double[] xpoints, double[] ypoints) {
        super(xpoints, ypoints);
    }

    public PolygonPanelItem(double[] xpoints, double[] ypoints, String id) {
        super(xpoints, ypoints, id);
    }

    public String getSrcImagePath() {
        return srcImagePath;
    }

    public void setSrcImagePath(String srcImagePath) {
        this.srcImagePath = srcImagePath;
    }

    public List<PolygonPanelItem> getPolygonPanelItems() {
        init();
        List<PolygonPanelItem> resultList = new ArrayList<>();
        Set<String> keySet = polygonPanelItemMap.keySet();
        keySet.forEach(key -> resultList.add(getItem(key)));
        return resultList;
    }

    public PolygonPanelItem getItem(String id) {
        init();
        PolygonPanelItem polygonPanelItem = polygonPanelItemMap.get(id);
        Vector vector = vectorMap.get(id);

        Point center = getCenter();
        double x0 = center.x + vector.getX();
        double y0 = center.y + vector.getY();

        Vector translateVector = PointUtil.getTranslateVector(polygonPanelItem.getCenter(), new Point(x0, y0));
        polygonPanelItem.translate(translateVector);

        if (angle != null) {
            polygonPanelItem.rotateTransform(getCenter(), angle);
            polygonPanelItem.setAngle((polygonPanelItem.getAngle() == null) ? angle : polygonPanelItem.getAngle() + angle);
        }

        return polygonPanelItem;
    }

    public void putItem(PolygonPanelItem item) {
        init();
        polygonPanelItemMap.put(item.getId(), item);
        vectorMap.put(item.getId(), PointUtil.getTranslateVector(this.getCenter(), item.getCenter()));
    }

    private void init() {
        if (polygonPanelItemMap == null) {
            polygonPanelItemMap = new HashMap<>();
        }
        if (vectorMap == null) {
            vectorMap = new HashMap<>();
        }
    }

    public Double getAngle() {
        return angle;
    }

    public void setAngle(Double angle) {
        this.angle = angle;
    }

    public Double getScale() {
        return scale;
    }

    public void setScale(Double scale) {
        this.scale = scale;
    }

    public Polygon getSrcPolygon() {
        return srcPolygon;
    }

    public void setSrcPolygon(Polygon srcPolygon) {
        this.srcPolygon = srcPolygon;
    }

    @Override
    public PolygonPanelItem clone() {
        PolygonPanelItem polygonPanelItem = (PolygonPanelItem) super.clone();
        polygonPanelItem.srcPolygon = this.srcPolygon.clone();
        polygonPanelItem.srcImagePath = this.srcImagePath;
        polygonPanelItem.angle = this.angle;
        polygonPanelItem.scale = this.scale;
        if (this.polygonPanelItemMap != null) {
            polygonPanelItem.polygonPanelItemMap = new HashMap<>();
            polygonPanelItem.polygonPanelItemMap.putAll(this.polygonPanelItemMap);
        }
        if (this.vectorMap != null) {
            polygonPanelItem.vectorMap = new HashMap<>();
            polygonPanelItem.vectorMap.putAll(this.vectorMap);
        }
        return polygonPanelItem;
    }
}
